<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of mmw_mapper_BookmarkMapper
 *
 * @author Manu
 */
require_once("mmw_mapper_Mapper.php");
require_once(dirname(__FILE__) . '/../domain/mmw_domain_Bookmark.php');
require_once(dirname(__FILE__) . '/../domain/mmw_domain_Webpage.php');
require_once(dirname(__FILE__) . '/../domain/mmw_domain_Tag.php');

class mmw_mapper_BookmarkMapper extends mmw_mapper_Mapper {

    private $insertIdsStmt;
    private $deleteIdsStmt;
    private $findByUrlStmt;
    private $findByTagLikeStmt;
    private $existsHasIdsStmt;
    private $existsHasWebpageIdStmt;
    private $existsHasTagIdStmt;

    public function __construct() {
        parent::__construct();

        $this->insertIdsStmt = self::$PDO->prepare(
                "INSERT INTO webpage_has_tags (webpage_id,tags_id) VALUES (?,?);");
        $this->deleteIdsStmt = self::$PDO->prepare(
                "DELETE FROM webpage_has_tags WHERE webpage_id=? AND tags_id=?");
        $this->findByUrlStmt = self::$PDO->prepare(
                "SELECT * FROM webpage,tags,webpage_has_tags  WHERE webpage.id=webpage_has_tags.webpage_id AND tags.id=webpage_has_tags.tags_id"
                . " AND (url=?) ORDER BY tag");
        $this->findByTagLikeStmt = self::$PDO->prepare(
                "SELECT * FROM webpage,tags,webpage_has_tags  WHERE webpage.id=webpage_has_tags.webpage_id AND tags.id=webpage_has_tags.tags_id"
                . " AND (tag LIKE ?) ORDER BY tag,title,url");
        $this->existsHasIdsStmt = self::$PDO->prepare(
                "SELECT count(*) FROM webpage_has_tags WHERE webpage_id=? AND tags_id=?");
        $this->existsHasWebpageIdStmt = self::$PDO->prepare(
                "SELECT count(*) FROM webpage_has_tags  WHERE webpage_id=?");
        $this->existsHasTagIdStmt = self::$PDO->prepare(
                "SELECT count(*) FROM webpage_has_tags WHERE tags_id=?");
    }

    public function insertIds($webpage_id, $tags_id) {
        $values = array($webpage_id, $tags_id);
        $this->insertIdsStmt->execute($values);
    }

    public function findByUrl($url) {
        $values = array($url);
        $this->findByUrlStmt->execute($values);
        $arResults = $this->findByUrlStmt->fetchAll(PDO::FETCH_ASSOC);
        $this->findByUrlStmt->closeCursor();

        if (!is_array($arResults)) {
            return null;
        }

        $arBookmarks = $this->createObjectArray($arResults);

        return $arBookmarks;
    }

    public function findByTagLike($tag) {
        $values = array('%' . $tag . '%');
        $this->findByTagLikeStmt->execute($values);
        $arResults = $this->findByTagLikeStmt->fetchAll(PDO::FETCH_ASSOC);
        $this->findByTagLikeStmt->closeCursor();

        if (!is_array($arResults)) {
            return null;
        }

        $arTags = $this->createObjectArray($arResults);
        return $arTags;
    }

    protected function createObject($array) {
        $bookmark = new mmw_domain_Bookmark();

        $webpage = new mmw_domain_Webpage();
        $webpage->setId($array['webpage_id']);
        $webpage->setUrl($array['url']);
        $webpage->setTitle($array['title']);

        $tag = new mmw_domain_Tag();
        $tag->setId($array['tags_id']);
        $tag->setTag($array['tag']);

        $bookmark->setWebpage($webpage);
        $bookmark->setTag($tag);

        return $bookmark;
    }

    public function existsHasIds($webpage_id, $tags_id) {
        $values = array($webpage_id, $tags_id);
        $this->existsHasIdsStmt->execute($values);
        $count = $this->existsHasIdsStmt->fetch();
        $this->existsHasIdsStmt->closeCursor();

        if ($count['count(*)'] > 0)
            return true;
        else
            return false;
    }

    public function existsHasWebpageId($webpage_id) {
        $values = array($webpage_id);
        $this->existsHasWebpageIdStmt->execute($values);
        $count = $this->existsHasWebpageIdStmt->fetch();
        $this->existsHasWebpageIdStmt->closeCursor();

        if ($count['count(*)'] > 0)
            return true;
        else
            return false;
    }

    public function existsHasTagId($tags_id) {
        $values = array($tags_id);
        $this->existsHasTagIdStmt->execute($values);
        $count = $this->existsHasTagIdStmt->fetch();
        $this->existsHasTagIdStmt->closeCursor();

        if ($count['count(*)'] > 0)
            return true;
        else
            return false;
    }

    public function deleteIds($webpage_id, $tags_id) {
        $values = array($webpage_id, $tags_id);
        $this->deleteIdsStmt->execute($values);
    }

}

?>
